<template>
  <div>
    <el-row style="display:flex;justify-content:space-between;margin-bottom:20px;">
      <selectorg @getselectorgid="getselectorgid" style></selectorg>
      <el-input
        clearable
        placeholder="人员姓名"
        style="width:20%;margin-left:25px;"
        v-model="searchData.username"
      ></el-input>

      <el-select
        v-model="searchData.sex"
        placeholder="性别"
        style="width:20%;margin-left:25px;"
        clearable
      >
        <el-option
          v-for="item in sexoptions"
          :key="item.value"
          :label="item.label"
          :value="item.label"
        ></el-option>
      </el-select>

      <el-select
        v-model="searchData.occupation"
        placeholder="请选择岗位"
        style="margin-left:25px;width:20%"
        clearable
      >
        <el-option
          v-for="item in occupationArr"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-select
        v-model="searchData.positional"
        placeholder="请选择职务"
        style="width:20%;margin-left:25px;"
        clearable
      >
        <el-option
          v-for="item in positionalArr"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_name"
        ></el-option>
      </el-select>
      <el-select
        v-model="searchData.tec_title"
        placeholder="请选择技术职称"
        style=";width:20%;margin-left:25px;"
        clearable
      >
        <el-option
          v-for="item in tecArr"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-select
        v-model="searchData.first_education"
        placeholder="请选择先学历"
        style=";width:20%;margin-left:25px;"
        clearable
      >
        <el-option
          v-for="item in educationArr"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-input
        clearable
        placeholder="请输入先学历专业"
        style="width:20%;margin-left:25px;"
        v-model="searchData.first_major"
      ></el-input>
    </el-row>
    <el-row style="display:flex;margin-bottom:30px">
      <span style="line-height:38px">年龄区间:</span>
      <div class="block" style="width:15%;margin:5px 30px 0 30px">
        <el-slider v-model="agearr" range :max="100" height="30"
          :marks="marks"
        ></el-slider>
      </div>
      <el-date-picker
        clearable
        v-model="entrttimearr"
        type="daterange"
        rangeSeparator="至"
        valueFormat="yyyy-MM-dd"
        style="margin-right:25px;"
        startPlaceholder="入职开始日期"
        endPlaceholder="入职开始日期"
      ></el-date-picker>

      <el-select
        v-model="searchData.usertype"
        multiple
        placeholder="人员状态"
        style="width:30%;margin-right:25px;"
        clearable
      >
        <el-option
          v-for="item in usetypeArr"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-button
        size="mini"
        type="primary"
        style="height:30px;margin-top:5px;"
        @click="getpersonlist(1)"
      >搜索</el-button>
    </el-row>
    <el-row>
      <el-button size="mini" type="success" icon="el-icon-plus" @click="shownewperson">新增</el-button>
      <el-button size="mini" type="primary" icon="el-icon-edit" @click="editperson">编辑</el-button>
      <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteperson">删除</el-button>
      <el-button size="mini" type="info" icon="el-icon-download" @click="daochu">导出</el-button>
    </el-row>
    <el-divider></el-divider>
    <persontable :tableData="tableData" :choosedp.sync="choosedp" :pagobj.sync="pagobj"></persontable>
    <persontable
      :tableData="newtableData"
      :choosedp.sync="choosedp"
      id="tableExcelcscs"
      v-show="false"
      :pagobj.sync="pagobj"
    ></persontable>
    <newperson v-if="isnewperson" :editdata="editdata" :isedit="isedit"></newperson>
  </div>
</template>

<script>
import persontable from '~/components/person/manager/persontable.vue';
import newperson from '~/components/person/manager/newperson.vue';
import selectorg from '~/components/common/selectorg.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  data() {
    return {
      isnewperson: false,
      tableData: [],
      choosedp: [],
      editdata: {},
      isedit: false,
      pagobj: {
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },

      ts: this.newStore(),
      searchData: {
        dept: '',
        sex: '',
        username: '',
        tec_title: '',
        positional: '',
        occupation: '',
        first_education: '',
        first_major: '',
        usertype: [],
      },
      entrttimearr: [],
      occupationArr: [],
      positionalArr: [],
      newtableData: [],
      usetypeArr: [],
      educationArr: [],
      agearr: [0, 100],
      marks: {
        0: '0',
        50: '50',
        100: '100',
      },
      sexoptions: [
        {
          label: '男',
        },
        {
          label: '女',
        },
      ],
      tecArr: [],
    };
  },
  created() {
    this.getcodeData('0202', '02', 'tecArr');
    this.getcodeData('0208', '02', 'occupationArr');
    this.getcodeData('0209', '02', 'positionalArr');
    this.getcodeData('0201', '02', 'educationArr');
    this.getusertypearr();
  },
  methods: {
    // 删除人员
    deleteperson() {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选人员',
        });
        return;
      }
      const deletearr = [];
      for (let i = 0; i < this.choosedp.length; i += 1) {
        deletearr.push(this.choosedp[i].hash.pid);
      }
      // console.log(deletearr, '2132');

      this.$confirm('确认删除已选择的人员?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const req = {
            handle:
              '33224548EFC2E94F19649E9176434E4460FE2E6FDE6D2EB360504B5A8F8A9E62',
            pid: deletearr,
          };
          this.spost(this.ts, '/mainservlet', req).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功',
            });
            this.getpersonlist();
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    // 编辑人员
    editperson() {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选人员',
        });
        return;
      }
      if (this.choosedp.length > 1) {
        this.$message({
          type: 'info',
          message: '只能编辑一条记录',
        });
        return;
      }
      this.isedit = true;
      const req = {
        handle:
          '33224548EFC2E94F97536D55316F44F2A06ED94DCB3C4375FD58B6E268D37EF8',
        pid: this.choosedp[0].hash.pid,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.editdata = ts.d.data.operation1[0].hash;
        this.reload('isnewperson');
      });
    },
    // 公共方法获取代码项
    getcodeData(supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
      });
    },
    getusertypearr() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '0204',
        codetype: '02',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.usetypeArr = ts.d.data.operation1;
        for (let i = 0; i < ts.d.data.operation1.length; i += 1) {
          if (ts.d.data.operation1[i].hash.code_id != '020402') {
            this.searchData.usertype.push(ts.d.data.operation1[i].hash.code_id);
          }
        }
        setTimeout(() => {
          this.getpersonlist();
        }, 200);
      });
    },
    // 公共组件刷新方法
    reload(partsname) {
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 点击新增人员
    shownewperson() {
      this.isedit = false;
      this.reload('isnewperson');
    },
    getselectorgid(val) {
      this.searchData.dept = val;
    },
    // 获取人员列表
    getpersonlist(p) {
      let utp = this.searchData.usertype.join(',');
      if (this.entrttimearr == null) {
        this.entrttimearr = [];
      }
      if (p) {
        this.pagobj.currentPage3 = p;
      }
      if (utp.length == 0) {
        utp = 'a';
      }
      const req = {
        handle:
          '33224548EFC2E94F0D1E4758898C9C3F8DEB003971634B53F6486B1D96ED9F7E',
        dept: this.searchData.dept,
        tec_title: this.searchData.tec_title,
        sex: this.searchData.sex,
        username: this.searchData.username,
        positional: this.searchData.positional,
        occupation: this.searchData.occupation,
        first_education: this.searchData.first_education,
        first_major: this.searchData.first_major,
        age_begin: this.agearr[0],
        age_end: this.agearr[1],
        usertype: utp,
        entry_time_begin: this.entrttimearr[0],
        entry_time_end: this.entrttimearr[1],
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: this.pagobj.pagesize,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.tableData = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
      this.listdatatable();
    },

    listdatatable() {
      let utp = this.searchData.usertype.join(',');
      if (this.entrttimearr == null) {
        this.entrttimearr = [];
      }
      if (utp.length == 0) {
        utp = 'a';
      }
      const req = {
        handle:
          '33224548EFC2E94F0D1E4758898C9C3F8DEB003971634B53F6486B1D96ED9F7E',
        dept: this.searchData.dept,
        tec_title: this.searchData.tec_title,
        sex: this.searchData.sex,
        username: this.searchData.username,
        positional: this.searchData.positional,
        occupation: this.searchData.occupation,
        first_education: this.searchData.first_education,
        first_major: this.searchData.first_major,
        age_begin: this.agearr[0],
        age_end: this.agearr[1],
        usertype: utp,
        entry_time_begin: this.entrttimearr[0],
        entry_time_end: this.entrttimearr[1],
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: 10000000000000,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.newtableData = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
    },
    // 导出功能
    daochu() {
      if (this.tableData.length == 0) {
        this.$message({
          type: 'error',
          message: '导出数据为空,无法导出',
        });
        return false;
      }
      const xlsxParam = { raw: true }; // 转换成excel时，使用原始的格式
      const wb = XLSX.utils.table_to_book(
        document.querySelector('#tableExcelcscs'),
        xlsxParam,
      );
      const wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array',
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream;charset=utf-8' }),
          'sheetjs.xlsx',
        );
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout);
      }
      return wbout;
    },
  },
  components: {
    persontable,
    newperson,
    selectorg,
  },
};
</script>
<style >
.el-slider__runway {
  margin: 7px 0;
}
</style>

